/**
 * 
 */
package com.xbang.attendance.web.action.warehouse.inventoryrecord;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.xbang.attendance.entity.stock.InventoryRecord;
import com.xbang.attendance.entity.system.Region;
import com.xbang.attendance.entity.system.Warehouse;
import com.xbang.attendance.service.InventoryService;
import com.xbang.attendance.service.RegionService;
import com.demo2do.lighturl.annotation.Action;
import com.xbang.core.dao.search.component.SearchComponent;
import com.xbang.core.struts.support.BaseActionSupport;
import com.xbang.core.utils.DateUtils;

/**
 * @author dev.zhang
 * 
 */
public class InventoryRecordManager extends BaseActionSupport {

	private static final long serialVersionUID = 5564954399670936978L;

	private final static Log logger = LogFactory.getLog(InventoryRecordManager.class);

	@Autowired
	private InventoryService inventoryService;

	private String salesOrderNumber;

	@Autowired
	private RegionService regionService; 
	
	private InventoryRecord inventoryRecord;

	private List<InventoryRecord> inventoryRecords = new ArrayList<InventoryRecord>();

	private List<Region> regions = new ArrayList<Region>();

	private Integer id, year, month;

	private Region region;

	private List<String> timeparts;

	private List<Warehouse> warehouses = new ArrayList<Warehouse>();
	
	private Date beginDate;
	private Date endDate;

	private String beginDateString;
	private String endDateString;
	

	@Action("/warehouse/invetnroyrecord/region/${id}")
	public String searchWarehouseByRegion() throws Exception {

		timeparts = inventoryService.getInventoryRecordTimePart();
		
		if (timeparts == null || timeparts.isEmpty()){
			String tmpTimepart = "" + DateUtils.getYear(new Date()) + "-" + DateUtils.getMonth(new Date());
			timeparts.add(tmpTimepart);
		}

		regions = regionService.getAKZRegion();

		if (id != null) {

			region = (Region) inventoryService.getObject(Region.class, id);
			String hqlSentence = "FROM Warehouse warehouse WHERE 1 = 1";

			SearchComponent searchComponent = new SearchComponent();
			searchComponent.addSearchElement("eq.warehouse.region.id", id);

			warehouses = inventoryService.searchForList(hqlSentence, searchComponent);
		}

		return "list-search";
	}

	@Action("/warehouse/inventoryrecord/order")
	public String order() throws Exception {
		return "order";
	}

	@Action("/warehouse/sales/salesorder/")
	public String viewSalesOrder() throws Exception {

		if (salesOrderNumber != null && !salesOrderNumber.isEmpty()) {
			String hqlSentence = "FROM InventoryRecord inventoryrecord WHERE 1 = 1";

			SearchComponent tmpSearchComponent = new SearchComponent();
			tmpSearchComponent.addSearchElement("eq.inventoryrecord.poNumber", salesOrderNumber);
			tmpSearchComponent.addSearchElement("orderBy.inventoryrecord.product.productName", "ASC");

			inventoryRecords = inventoryService.searchForList(hqlSentence, new HashMap());

			if (!inventoryRecords.isEmpty()) {
				inventoryRecord = inventoryRecords.get(0);
			}

		} else {
			return "r:/warehouse/inventoryrecord/list/search";
		}

		return "sales-order";
	}

	@Action("/warehouse/inventoryrecord/${id}/view")
	public String view() throws Exception {
		if (inventoryRecord == null && id != null) {
			inventoryRecord = inventoryService.getInventoryRecordByKey(id);
		}
		return "view";
	}

	@Action("/warehouse/inventoryrecord/${id}/delete")
	public String delete() throws Exception {
		if (id != null) {
			inventoryService.delete(InventoryRecord.class, id);
		}
		return "r:/warehouse/inventoryrecord/list";
	}

	/**
	 * @return the inventoryRecord
	 */
	public InventoryRecord getInventoryRecord() {
		return inventoryRecord;
	}

	/**
	 * @param inventoryRecord
	 *            the inventoryRecord to set
	 */
	public void setInventoryRecord(InventoryRecord inventoryRecord) {
		this.inventoryRecord = inventoryRecord;
	}

	/**
	 * @return the id
	 */
	public Integer getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(Integer id) {
		this.id = id;
	}

	/**
	 * @param inventoryService
	 *            the inventoryService to set
	 */
	public void setInventoryService(InventoryService inventoryService) {
		this.inventoryService = inventoryService;
	}

	/**
	 * @return the inventoryRecords
	 */
	public List<InventoryRecord> getInventoryRecords() {
		return inventoryRecords;
	}

	/**
	 * @param inventoryRecords
	 *            the inventoryRecords to set
	 */
	public void setInventoryRecords(List<InventoryRecord> inventoryRecords) {
		this.inventoryRecords = inventoryRecords;
	}

	/**
	 * @return the salesOrderNumber
	 */
	public String getSalesOrderNumber() {
		return salesOrderNumber;
	}

	/**
	 * @param salesOrderNumber
	 *            the salesOrderNumber to set
	 */
	public void setSalesOrderNumber(String salesOrderNumber) {
		this.salesOrderNumber = salesOrderNumber;
	}

	/**
	 * @return the warehouses
	 */
	public List<Warehouse> getWarehouses() {
		return warehouses;
	}

	/**
	 * @param warehouses
	 *            the warehouses to set
	 */
	public void setWarehouses(List<Warehouse> warehouses) {
		this.warehouses = warehouses;
	}

	/**
	 * @return the regions
	 */
	public List<Region> getRegions() {
		return regions;
	}

	/**
	 * @param regions
	 *            the regions to set
	 */
	public void setRegions(List<Region> regions) {
		this.regions = regions;
	}

	/**
	 * @return the region
	 */
	public Region getRegion() {
		return region;
	}

	/**
	 * @param region
	 *            the region to set
	 */
	public void setRegion(Region region) {
		this.region = region;
	}

	/**
	 * @return the timeparts
	 */
	public List<String> getTimeparts() {
		return timeparts;
	}

	/**
	 * @param timeparts
	 *            the timeparts to set
	 */
	public void setTimeparts(List<String> timeparts) {
		this.timeparts = timeparts;
	}

	/**
	 * @return the year
	 */
	public Integer getYear() {
		return year;
	}

	/**
	 * @param year
	 *            the year to set
	 */
	public void setYear(Integer year) {
		this.year = year;
	}

	/**
	 * @return the month
	 */
	public Integer getMonth() {
		return month;
	}

	/**
	 * @param month
	 *            the month to set
	 */
	public void setMonth(Integer month) {
		this.month = month;
	}

	public Date getBeginDate() {
		return beginDate;
	}

	public void setBeginDate(Date beginDate) {
		this.beginDate = beginDate;
	}

	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	public String getBeginDateString() {
		return DateUtils.format(beginDate, "yyyy-MM-dd");
	}

	public void setBeginDateString(String beginDateString) {
		this.beginDateString = beginDateString;
	}

	public String getEndDateString() {
		return DateUtils.format(endDate, "yyyy-MM-dd");
	}

	public void setEndDateString(String endDateString) {
		
		this.endDateString =endDateString;
	}

}
